﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YelpSharp;
using YelpSharp.Data.Options;

public class YelpController : Controller {

    private readonly Yelp _client;

    public YelpController() {
        var options = new Options() {
            AccessToken = "",
            AccessTokenSecret = "",
            ConsumerKey = "",
            ConsumerSecret = ""
        };

        _client = new Yelp(options);
    }

    public YelpController(Yelp client) {
        _client = client;
    }

    public ActionResult Places(string postalCode,
                    string category, string term) {

        var so = new SearchOptions() {
            LocationOptions = new LocationOptions() {
                location = postalCode
            },
            GeneralOptions = new GeneralOptions() {
                category_filter = category,
                term = term
            }
        };
        var results = _client.Search(so);

        return Json(results.Result.businesses
            .Where(p => p.name.StartsWith(term))
            .Select(
            p => new {
                value = p.location.coordinate.latitude + "~"
                   + p.location.coordinate.longitude,
                label = p.name
            }).ToArray()
            , JsonRequestBehavior.AllowGet);
    }

    public ActionResult Categories(string postalCode,
                    string categories, string term) {

        var categoryList = new List<string> { 
			"Restaurants", "Nightlife", "Arts"
		};

        var results = string.IsNullOrEmpty(term) ?
            categoryList : categoryList
                .Where(c =>
                    c.ToLower()
                    .Contains(term.ToLower()));
        return Json(results.Select(
            c => new { value = c.ToLower(), label = c })
            .ToArray(), JsonRequestBehavior.AllowGet);
    }
}
